﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace Albatross.WPF.Common.Views {

	public class VisibilityBooleanConverter : IValueConverter {
		public VisibilityBooleanConverter() {
			_collapsedOrHidden = Visibility.Collapsed;
		}

		/// <summary>
		/// usually if the boolean value is true, the convert return Visible.
		/// if Reversed is true, it will return CollapseOrHidden instead
		/// </summary>
		public bool Reversed {
			get;
			set;
		}

		private Visibility _collapsedOrHidden;
		public Visibility CollapsedOrHidden {
			get { return _collapsedOrHidden; }
			set {
				if (value == Visibility.Visible) {
					throw new ArgumentException();
				}
				_collapsedOrHidden = value;
			}
		}

		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {

			if (value is Visibility){
				if (Reversed) {
					return (Visibility)value != Visibility.Visible;
				} else {
					return (Visibility)value == Visibility.Visible;
				}
			}

			return false;
		}

		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			if (value is bool) {
				if ((bool)value) {
					return Reversed ? CollapsedOrHidden :  Visibility.Visible;
				}
			}
			return CollapsedOrHidden;
		}
	}
}
